//
//  BPDiscoverSearch.swift
//  BPWeiBo
//
//  Created by apple on 16/4/6.
//  Copyright © 2016年 apple. All rights reserved.
/*
case ScaleToFill   放大或者缩小进行填充
case ScaleAspectFit
case ScaleAspectFill 工作中一般使用这个

*/

import UIKit

class BPDiscoverSearch: UIView {
    
    
    @IBInspectable 

    @IBOutlet weak var cancleBtn: UIButton!
    //右边的约束
    @IBOutlet weak var rightLayout: NSLayoutConstraint!
    @IBOutlet weak var searchFiled: UITextField!
    
    
    
    
    //MARK:开始编辑调用的方法
    @IBAction func startEdit(sender: AnyObject) {
        
        //1.修改约束
        //系统只是收集了约束的变化 会在layoutSubVIew中更新界面
        rightLayout.constant = self.cancleBtn.frame.width
        //2.在动画闭包中强制执行动画
        //系统默认的动画时间是0.25秒
        UIView.animateWithDuration(0.25) { () -> Void in
            //提前刷新界面,根据 自动布局 来修改frame
            self.layoutIfNeeded()
        }
    }
    
    
    //MARK:取消按钮的点击事件
    @IBAction func cancleDidClick(sender: AnyObject) {
        
        rightLayout.constant = 0
        
        UIView.animateWithDuration(0.25) { () -> Void in
            //提前刷新界面,根据 自动布局 来修改frame
            self.layoutIfNeeded()
        }
        
        //取消键盘的第一响应者
        searchFiled.resignFirstResponder()
    }
    
    
    
    //类方法
    class func loadSearchView() -> BPDiscoverSearch {
        let v = NSBundle.mainBundle().loadNibNamed("BPDiscoverSearch", owner: nil, options: nil).last as! BPDiscoverSearch
        
        return v
    }
    
    override func awakeFromNib() {
        
        self.frame = CGRectMake(0, 0, UIScreen.mainScreen().bounds.width,self.frame.height)
        
        //获取文本输入框左边的视图
        self.searchFiled.leftView = UIImageView(image: UIImage(named: "searchbar_textfield_search_icon"))
        //设置始终显示
        self.searchFiled.leftViewMode = .Always
        //设置显示模式
        self.searchFiled.leftView?.contentMode = .Center
        //设置输入框为为圆角
        //self.searchFiled.layer.cornerRadius  = 10
        self.searchFiled.layer.masksToBounds = true
        //设置frame
        self.searchFiled.leftView?.frame = CGRectMake(0, 0, self.bounds.height, self.bounds.height)
    }


}
